package FIT::Document::Transformer::AddColumns;

use FIT::Class;
use FIT::Document::Parser::Twig;

extends 'FIT::Document::Transformer::Base';

has columns => (is => 'ro', isa => 'ArrayRef', required => 1);

sub _transform {
    my $table = shift;
    my @columns = @{ $self->columns };
    $table->get_body; # normalize with a body
    my $first_cell = $table->first_cell;
    $first_cell->set_att(colspan => ($first_cell->att('colspan') + @columns));

    for my $column (@columns) {
        my $row = $table->second_row;
        my ($header_text, $cell_text) = @$column;

        my $cell = XML::Twig::Elt->new('td');
        $cell->paste(last_child => $row);
        my $xml = "<i>". $self->escape( $header_text ). "</i>";
        $cell->set_inner_xml($xml);

        while ($row = $row->next_row) {
            my $cell = XML::Twig::Elt->new('td');
            $cell->paste(last_child => $row);
            $cell->set_text($cell_text);
        }
    }
}
